package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.service.IInterpolateService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/interpolate")
public class InterpolateController {

    @Resource
    IInterpolateService interpolateService;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    /**
     * 用于内推
     *
     * @param instanceId   实例id
     * @param token        jwt令牌
     * @param introduction 推荐信息
     * @param attachment   附件
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('interpolate-interpolate')")
    @DS("#header.instanceid")
    @PostMapping("interpolate")
    public AjaxResponse interpolate(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, String introduction, @RequestParam("attachment") List<String> attachment) throws CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        HashMap<String, Object> resultMap = interpolateService.interpolate(userId, introduction, attachment);
        return AjaxResponse.success(resultMap);
    }
}
